home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / twnsck12.zip / SRC\COMMS.C < prev    next >
Text File  |  1994-12-04  |  8KB  |  314 lines

  1. /*
  2.  *  TwinSock - "Troy's Windows Sockets"
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20. #include <windows.h>
  21. #include <stdio.h>
  22.  
  23. extern    HINSTANCE    hinst;
  24. int    iPortChanged = 0;
  25. static    char    achStartPort[256];
  26.  
  27. static    UINT
  28. GetConfigInt(char const *pchItem, UINT nDefault)
  29. {
  30.     return GetPrivateProfileInt("Config", pchItem, nDefault, "TWINSOCK.INI");
  31. }
  32.  
  33. void
  34. InitComm(int    idComm)
  35. {
  36.     DCB    dcb;
  37.  
  38.     dcb.Id = idComm;
  39.     dcb.BaudRate = GetConfigInt("Speed", 19200);
  40.     dcb.ByteSize = GetConfigInt("Databits", 8);
  41.     dcb.Parity = GetConfigInt("Parity", NOPARITY);
  42.     dcb.StopBits = GetConfigInt("StopBits", ONESTOPBIT);
  43.     dcb.RlsTimeout = GetConfigInt("RlsTimeout", 0);
  44.     dcb.CtsTimeout = GetConfigInt("CtsTimeout", 0);
  45.     dcb.DsrTimeout = GetConfigInt("DsrTimeout", 0);
  46.     dcb.fBinary = TRUE;
  47.     dcb.fRtsDisable = GetConfigInt("fRtsDisable", FALSE);
  48.     dcb.fParity = GetConfigInt("fParity", FALSE);
  49.     dcb.fOutxCtsFlow = GetConfigInt("OutxCtsFlow", TRUE);
  50.     dcb.fOutxDsrFlow = GetConfigInt("OutxDsrFlow", FALSE);
  51.     dcb.fDummy = 0;
  52.     dcb.fDtrDisable = GetConfigInt("fDtrDisable", FALSE);
  53.     dcb.fOutX = GetConfigInt("fOutX", TRUE);
  54.     dcb.fInX = GetConfigInt("fInX", FALSE);
  55.     dcb.fPeChar = 0;
  56.     dcb.fNull = 0;
  57.     dcb.fChEvt = 0;
  58.     dcb.fDtrflow = GetConfigInt("fDtrFlow", FALSE);
  59.     dcb.fRtsflow = GetConfigInt("fRtsFlow", FALSE);
  60.     dcb.fDummy2 = 0;
  61.     dcb.XonChar = '\021';
  62.     dcb.XoffChar = '\023';
  63.     dcb.XonLim = 100;
  64.     dcb.XoffLim = 900;
  65.     dcb.PeChar = 0;
  66.     dcb.EofChar = 0;
  67.     dcb.EvtChar = 0;
  68.     dcb.TxDelay = 0;
  69.  
  70.     SetCommState(&dcb);
  71. }
  72.  
  73. static    struct
  74. {
  75.     unsigned iValue;
  76.     long    iSpeed;
  77. } aSpeeds[] =
  78. {
  79.     { CBR_110,    110    },
  80.     { CBR_300,    300    },
  81.     { CBR_600,    600    },
  82.     { CBR_1200,    1200    },
  83.     { CBR_2400,    2400    },
  84.     { CBR_4800,    4800    },
  85.     { CBR_9600,    9600    },
  86.     { CBR_14400,    14400    },
  87.     { CBR_19200,    19200    },
  88.     { CBR_38400,    38400    },
  89.     { CBR_56000,    56000    },
  90.     { CBR_128000,    128000    },
  91.     { CBR_256000,    2256000    },
  92.     { 0,        -1    },
  93. };
  94.  
  95. static    char    *apchPorts[] =
  96. {
  97.     "COM1",
  98.     "COM2",
  99.     "COM3",
  100.     "COM4",
  101.     0
  102. };
  103.  
  104. char    *apchParities[] =
  105. {
  106.     "None",
  107.     "Odd",
  108.     "Even",
  109.     "Mark",
  110.     "Space",
  111.     0
  112. };
  113.  
  114. char    *apchStopBits[] =
  115. {
  116.     "1",
  117.     "1.5",
  118.     "2",
  119.     0
  120. };
  121.  
  122. #define    CE_PORT     101
  123. #define    CE_SPEED    103
  124. #define    CE_DATABITS    105
  125. #define    CE_PARITY    107
  126. #define    CE_STOPBITS    109
  127.  
  128. static    void
  129. FillCommsDialog(HWND hDlg)
  130. {
  131.     int    i;
  132.     long    iSpeedNow;
  133.     char    achTmp[100];
  134.  
  135.     GetPrivateProfileString("Config", "Port", "COM1", achTmp, 100, "TWINSOCK.INI");
  136.     SetDlgItemText(hDlg, CE_PORT, achTmp);
  137.     strcpy(achStartPort, achTmp);
  138.     for (i = 0; apchPorts[i]; i++)
  139.         SendDlgItemMessage(hDlg, CE_PORT, CB_ADDSTRING, 0, (LPARAM) apchPorts[i]);
  140.     iSpeedNow = GetPrivateProfileInt("Config", "Speed", 19200, "TWINSOCK.INI");
  141.     for (i = 0; aSpeeds[i].iSpeed != -1; i++)
  142.     {
  143.         sprintf(achTmp, "%ld", aSpeeds[i].iSpeed);
  144.         SendDlgItemMessage(hDlg, CE_SPEED, CB_ADDSTRING, 0, (LPARAM) achTmp);
  145.         if (aSpeeds[i].iSpeed == iSpeedNow ||
  146.             aSpeeds[i].iValue == iSpeedNow)
  147.         {
  148.             SendDlgItemMessage(hDlg, CE_SPEED, CB_SETCURSEL, i, 0);
  149.         }
  150.     }
  151.  
  152.     for (i = 5; i <= 8; i++)
  153.     {
  154.         sprintf(achTmp, "%d", i);
  155.         SendDlgItemMessage(hDlg, CE_DATABITS, CB_ADDSTRING, 0, (LPARAM) achTmp);
  156.     }
  157.     i = GetPrivateProfileInt("Config", "DataBits", 8, "TWINSOCK.INI");
  158.     SendDlgItemMessage(hDlg, CE_DATABITS, CB_SETCURSEL, i - 5, 0);
  159.  
  160.     for (i = 0; apchParities[i]; i++)
  161.         SendDlgItemMessage(hDlg, CE_PARITY, CB_ADDSTRING, 0, (LPARAM) apchParities[i]);
  162.     i = GetPrivateProfileInt("Config", "Parity", 0, "TWINSOCK.INI");
  163.     SendDlgItemMessage(hDlg, CE_PARITY, CB_SETCURSEL, i, 0);
  164.  
  165.     for (i = 0; apchStopBits[i]; i++)
  166.         SendDlgItemMessage(hDlg, CE_STOPBITS, CB_ADDSTRING, 0, (LPARAM) apchStopBits[i]);
  167.     i = GetPrivateProfileInt("Config", "StopBits", 0, "TWINSOCK.INI");
  168.     SendDlgItemMessage(hDlg, CE_STOPBITS, CB_SETCURSEL, i, 0);
  169. }
  170.  
  171. void
  172. ReadCommsDialog(HWND hDlg)
  173. {
  174.     int    i;
  175.     char    achTemp[100];
  176.  
  177.     GetDlgItemText(hDlg, CE_PORT, achTemp, 100);
  178.     WritePrivateProfileString("Config", "Port", achTemp, "TWINSOCK.INI");
  179.     if (stricmp(achTemp, achStartPort))
  180.         iPortChanged = 1;
  181.  
  182.     i = SendDlgItemMessage(hDlg, CE_SPEED, CB_GETCURSEL, 0, 0);
  183.     i = aSpeeds[i].iValue;
  184.     sprintf(achTemp, "%d", i);
  185.     WritePrivateProfileString("Config", "Speed", achTemp, "TWINSOCK.INI");
  186.  
  187.     i = SendDlgItemMessage(hDlg, CE_DATABITS, CB_GETCURSEL, 0, 0);
  188.     i += 5;
  189.     sprintf(achTemp, "%d", i);
  190.     WritePrivateProfileString("Config", "Databits", achTemp, "TWINSOCK.INI");
  191.  
  192.     i = SendDlgItemMessage(hDlg, CE_PARITY, CB_GETCURSEL, 0, 0);
  193.     sprintf(achTemp, "%d", i);
  194.     WritePrivateProfileString("Config", "Parity", achTemp, "TWINSOCK.INI");
  195.  
  196.     i = SendDlgItemMessage(hDlg, CE_STOPBITS, CB_GETCURSEL, 0, 0);
  197.     sprintf(achTemp, "%d", i);
  198.     WritePrivateProfileString("Config", "StopBits", achTemp, "TWINSOCK.INI");
  199. }
  200.  
  201. #pragma argsused
  202. BOOL    CALLBACK
  203. CommsDlgProc(    HWND    hDlg,
  204.         UINT    wMsg,
  205.         WPARAM    wParam,
  206.         LPARAM    lParam)
  207. {
  208.     switch(wMsg)
  209.     {
  210.     case WM_INITDIALOG:
  211.         FillCommsDialog(hDlg);
  212.         return TRUE;
  213.  
  214.     case WM_COMMAND:
  215.         switch(wParam)
  216.         {
  217.         case IDOK:
  218.             ReadCommsDialog(hDlg);
  219.             EndDialog(hDlg, TRUE);
  220.             break;
  221.  
  222.         case IDCANCEL:
  223.             EndDialog(hDlg, FALSE);
  224.             break;
  225.         }
  226.         break;
  227.     }
  228.     return FALSE;
  229. }
  230.  
  231. BOOL
  232. CommsEdit(HWND hwndParent)
  233. {
  234.     FARPROC    fpDlgProc;
  235.     BOOL    bStatus;
  236.  
  237.     fpDlgProc = MakeProcInstance((FARPROC) CommsDlgProc, hinst);
  238.     bStatus = DialogBox(hinst, "COMMS_DLG", hwndParent, fpDlgProc);
  239.     FreeProcInstance(fpDlgProc);
  240.     return bStatus;
  241. }
  242.  
  243.  
  244.  
  245. #define    DL_NUMBER    101
  246. #define    DL_PULSE    102
  247. #define    DL_TONE        103
  248.  
  249. #pragma argsused
  250. BOOL    CALLBACK
  251. DialDlgProc(    HWND    hDlg,
  252.         UINT    wMsg,
  253.         WPARAM    wParam,
  254.         LPARAM    lParam)
  255. {
  256.     int    iMethod;
  257.     char    achNumber[80];
  258.  
  259.     switch(wMsg)
  260.     {
  261.     case WM_INITDIALOG:
  262.         iMethod = GetPrivateProfileInt("Config", "DialingMethod", 1, "TWINSOCK.INI");
  263.         SendMessage(GetDlgItem(hDlg, iMethod ? DL_TONE : DL_PULSE), BM_SETCHECK, 1, 0);
  264.         GetPrivateProfileString("Config", "LastNumber", "", achNumber, 80, "TWINSOCK.INI");
  265.         SetDlgItemText(hDlg, DL_NUMBER, achNumber);
  266.         EnableWindow(GetDlgItem(hDlg, IDOK),
  267.             (GetDlgItemText(hDlg, DL_NUMBER, achNumber, 80) != 0));
  268.         return TRUE;
  269.  
  270.     case WM_COMMAND:
  271.         switch(wParam)
  272.         {
  273.         case IDOK:
  274.             GetDlgItemText(hDlg, DL_NUMBER, achNumber, 80);
  275.             WritePrivateProfileString("Config", "LastNumber", achNumber, "TWINSOCK.INI");
  276.             iMethod = SendMessage(GetDlgItem(hDlg, DL_TONE), BM_GETCHECK, 0, 0);
  277.             WritePrivateProfileString("Config", "DialingMethod", iMethod ? "1" : "0", "TWINSOCK.INI");
  278.             EndDialog(hDlg, TRUE);
  279.             SendData(iMethod ? "\rATDT" : "\rATDP", 5);
  280.             SendData(achNumber, strlen(achNumber));
  281.             SendData("\r", 1);
  282.             break;
  283.  
  284.         case IDCANCEL:
  285.             EndDialog(hDlg, FALSE);
  286.             break;
  287.  
  288.         case DL_NUMBER:
  289.             EnableWindow(GetDlgItem(hDlg, IDOK),
  290.                 (GetDlgItemText(hDlg, DL_NUMBER, achNumber, 80) != 0));
  291.             break;
  292.  
  293.         }
  294.         break;
  295.     }
  296.     return FALSE;
  297. }
  298.  
  299. BOOL
  300. DialNumber(HWND hwndParent)
  301. {
  302.     FARPROC    fpDlgProc;
  303.     BOOL    bStatus;
  304.  
  305.     fpDlgProc = MakeProcInstance((FARPROC) DialDlgProc, hinst);
  306.     bStatus = DialogBox(hinst, "DIAL_DLG", hwndParent, fpDlgProc);
  307.     FreeProcInstance(fpDlgProc);
  308.     return bStatus;
  309. }
  310.  
  311.  
  312.  
  313.  
  314.